import storage from "@/utils/Storage"
import { getSearchkeywordsAPI } from "@/views/username/api"
import { getUserPlaylisAPI } from "@/views/musicLibrary/api"
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
import { LoginQrCheckAPI, LoginQrCreateAPI, LoginQrKeyAPI, userAccountAPI } from "@/views/account/api"
import { RootState } from ".."
import { feachPlaylistDetailAction } from "@/views/musicLibrary/store"

export const feachSearchKeywordsAction = createAsyncThunk("searchkeywords", async (IptValue: string, { dispatch }) => {
  const { result } = await getSearchkeywordsAPI(IptValue)
  dispatch(changeSearchUsersAction(result.userprofiles))
})

interface IchangeUserArg {
  modeType: string,
  userInfo: any
}
export const feachChangeUserInfoAction = createAsyncThunk<void, IchangeUserArg>("changeUserInfo", async (arg, { dispatch }) => {
  const { modeType, userInfo } = arg
  // 预留
  const { playlist } = await getUserPlaylisAPI(userInfo.userId)
  storage.set("data", { modeType, user: userInfo, likedSongPlaylistID: playlist[0].id })
  dispatch(changeUserInfoAction({ modeType, userInfo, likedSongPlaylistID: playlist[0].id }))
})

export const feachAccountQrAction = createAsyncThunk("accountQr", async (arg, { dispatch }) => {
  // 获取qrkey 
  const { data } = await LoginQrKeyAPI()
  const res = await LoginQrCreateAPI(data.unikey)
  dispatch(changeQrUrlAction({ key: data.unikey, url: res.data.qrurl }))
})

export const feachExamineQrAction = createAsyncThunk<void, any, { state: RootState }>("examineqr", async (arg, { dispatch, getState }) => {
  const qrunikey = getState().login.qrunikey
  const { code, cookie } = await LoginQrCheckAPI(qrunikey)
  dispatch(changeQrCodeAndStateAction({ code, cookie }))
})

export const feachLoginQrAction = createAsyncThunk<void, string>("loginQr", async (arg, { dispatch }) => {
  const { account, profile } = await userAccountAPI()
  storage.set("data", { modeType: arg, user: profile, likedSongPlaylistID: account.id })
  dispatch(changeUserInfoAction({ modeType: arg, userInfo: profile, likedSongPlaylistID: account.id }))
})

interface IState {
  userInfo: any,
  likedSongPlaylistID: number,
  loginMode: string,
  searchUsers: any[],
  qrunikey: string,
  qrUrl: string,
  qrCode: number,
  cookie: string
}

const initialState: IState = {
  userInfo: storage.get("data")?.user ?? {},
  likedSongPlaylistID: storage.get("data")?.likedSongPlaylistID ?? "",
  loginMode: storage.get("data")?.modeType ?? "",
  searchUsers: [],
  qrunikey: "",
  qrUrl: "",
  qrCode: 801,
  cookie: ""
}

const LoginSlice = createSlice({
  name: 'login',
  initialState,
  reducers: {
    changeSearchUsersAction(state, { payload }) {
      state.searchUsers = payload
    },
    changeUserInfoAction(state, { payload }) {
      const { modeType, userInfo, likedSongPlaylistID } = payload
      state.loginMode = modeType
      state.userInfo = userInfo
      state.likedSongPlaylistID = likedSongPlaylistID
    },
    changeQrUrlAction(state, { payload }) {
      const { url, key } = payload
      state.qrUrl = url
      state.qrunikey = key
    },
    changeQrCodeAndStateAction(state, { payload }) {
      const { code, cookie } = payload
      state.qrCode = code
      if (code === 803) {
        state.cookie = cookie
      }
    }
  }
})

export const { changeSearchUsersAction, changeUserInfoAction, changeQrUrlAction, changeQrCodeAndStateAction } = LoginSlice.actions
export default LoginSlice.reducer